package io.wdb.modules.generator.generator.service.impl.operation;

import java.math.BigInteger;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;

import io.wdb.common.utils.PageHelperUtil;
import io.wdb.common.utils.Query;
import io.wdb.modules.generator.generator.dao.BookEntityDao;
import io.wdb.modules.generator.generator.dao.BookgridInfoDao;
import io.wdb.modules.generator.generator.dao.OrderTransDao;
import io.wdb.modules.generator.generator.entity.BookEntityEntity;
import io.wdb.modules.generator.generator.entity.BookgridInfoEntity;
import io.wdb.modules.generator.generator.entity.OrderTransEntity;
import io.wdb.modules.generator.generator.entity.customer.BookBorrowStatistics;
import io.wdb.modules.generator.generator.entity.customer.BookEntityCustomer;
import io.wdb.modules.generator.generator.entity.customer.OrderTransEntityCustomer;
import io.wdb.modules.generator.generator.entity.web.ResponseEnum;
import io.wdb.modules.generator.generator.entity.web.WebResponse;
import io.wdb.modules.generator.generator.service.operation.BookBorrowOperationService;

@Service
public class BookBorrowServiceImpl implements BookBorrowOperationService{

	@Autowired
	private OrderTransDao orderTransDao;
	
	@Autowired
	private BookEntityDao bookEntityDao;
	
	@Autowired
	private BookgridInfoDao bookgridInfoDao;
	
	@Override
	public List<OrderTransEntityCustomer> queryList(Query query) {
		PageHelper.startPage(PageHelperUtil.getPageNum(query),PageHelperUtil.getPageSize(query));
		List<OrderTransEntityCustomer> orderTransEntityCustomers =  orderTransDao.queryListBorrow(query);
		return orderTransEntityCustomers;
	}

	@Override
	public int queryTotal(Query query) {
		return bookEntityDao.queryTotalBorrow(query);
	}

	@Override
	public WebResponse updateBorrowStatus(BookEntityCustomer bookEntityCustomer) {
		WebResponse webResponse = new WebResponse();
		if(bookEntityCustomer.getBorrowedStatus()==0) {
			//更改用户借阅流水状态
			OrderTransEntity orderTransEntity = orderTransDao.queryBorrowBook(bookEntityCustomer.getRfidCode());
			if(orderTransEntity==null) {
				webResponse.setCode(ResponseEnum.USER_CURRENT_BORROW_BOOK.getCode());
				webResponse.setMessage(ResponseEnum.USER_CURRENT_BORROW_BOOK.getMessage());
				return webResponse;
			}else {
				//根据书籍查询当前书格和书柜编号
				BookEntityEntity bookEntity = bookEntityDao.queryObject(bookEntityCustomer.getRfidCode());
				BookgridInfoEntity bookgridInfoEntity = bookgridInfoDao.queryObject(bookEntity.getCurrentGridCode());
				//修改借阅流水状态
				orderTransEntity.setTransStatus(30);
				orderTransEntity.setRestituteGridCode(bookEntity.getCurrentGridCode());
				orderTransEntity.setRestituteShelfCode(bookgridInfoEntity.getBookshelfCode());
				orderTransDao.update(orderTransEntity);
				//修改书籍状态
				bookEntity.setBorrowedStatus(0);
				bookEntityDao.update(bookEntity);
			}
		}else if(bookEntityCustomer.getBorrowedStatus()==2||bookEntityCustomer.getBorrowedStatus()==1) {
			bookEntityDao.update(bookEntityCustomer);
		}
		return null;
	}

	@Override
	public WebResponse updateBookStatus(BookEntityEntity bookEntityEntity) {
		BookEntityEntity bookEntityEntityDb = bookEntityDao.queryObject(bookEntityEntity.getRfidCode());
		if(bookEntityEntity.getStatus()!=null) {
			bookEntityEntityDb.setStatus(bookEntityEntity.getStatus());
		}
		if(bookEntityEntity.getSellStatus()!=null&&!"".equals(bookEntityEntity.getSellStatus())) {
			bookEntityEntityDb.setSellStatus(bookEntityEntity.getSellStatus());
		}
		bookEntityDao.update(bookEntityEntityDb);
		return null;
	}

	@Override
	public List<OrderTransEntityCustomer> queryBorrowCount(Query query) {
		PageHelper.startPage(PageHelperUtil.getPageNum(query),PageHelperUtil.getPageSize(query));
		return orderTransDao.queryBorrowCount(query);
	}

	@Override
	public BookBorrowStatistics bookBorrowStatistics(String rfidCode) {
		List<OrderTransEntityCustomer> customers = orderTransDao.queryBorrowStatistics(rfidCode);
		Set<Long> setPerson = new HashSet<Long>();
		BookBorrowStatistics bookBorrowStatistics = new BookBorrowStatistics();
		BigInteger timeCountSecond = new BigInteger("0");
		for(OrderTransEntityCustomer orderTransEntityCustomer:customers) {
			setPerson.add(orderTransEntityCustomer.getUserId());
			timeCountSecond = timeCountSecond.add(new BigInteger(orderTransEntityCustomer.getTimeCount()));
		}
		timeCountSecond = timeCountSecond.divide(new BigInteger("3600"));
		bookBorrowStatistics.setBorrowCount(customers.size());
		bookBorrowStatistics.setBorrowPerson(setPerson.size());
		bookBorrowStatistics.setBorrowTimeCount(timeCountSecond.longValue());
		return bookBorrowStatistics;
	}

}
